//
//  ZGLSubscribeCollectionViewViewCell.m
//  Faker
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLSubscribeCollectionViewViewCell.h"
#import "ZGLOptionsModel.h"
#import <UIImageView+AFNetworking.h>
#import "ZGLHTTPTOOL.h"

@interface ZGLSubscribeCollectionViewViewCell ()

@property (nonatomic, strong) UIImageView *optionView;

@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UILabel *linnsLabel;

@end

@implementation ZGLSubscribeCollectionViewViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.optionView = [[UIImageView alloc] init];
        self.titleLabel = [[UILabel alloc] init];
        self.lineLabel = [[UILabel alloc] init];
        self.linnsLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.optionView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.linnsLabel];
        [self.contentView addSubview:self.lineLabel];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];

    [self.titleLabel setFrame:CGRectMake(0, layoutAttributes.frame.size.height * 2 / 3, layoutAttributes.frame.size.width, ScaleY * 20)];
    [self.optionView setFrame:CGRectMake(0, 0, ScaleX * 125, ScaleX * 125)];
    self.titleLabel.textAlignment = 1;
    [self.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [self.lineLabel setFrame:CGRectMake(ScaleX * 124.7, 0, ScaleX * 0.3, ScaleX * 125)];
    [self.linnsLabel setFrame:CGRectMake(0, ScaleX * 124.7, ScaleX * 125, ScaleX * 0.3)];

    self.linnsLabel.backgroundColor = [UIColor lightGrayColor];
    self.lineLabel.backgroundColor = [UIColor lightGrayColor];
}

- (void)setDic:(NSDictionary *)dic {
    if (_dic != dic) {
        _dic = dic;
    }
    self.titleLabel.text = dic[@"title"];
    [self createOptionItemData:dic];
}


- (void)createOptionItemData:(NSDictionary *)dic {
        __weak typeof(self) weakSelf = self;
        self.optionView.tintColor = [self getColor:dic[@"block_color"]];
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:dic[@"large_pic"]]];
        [self.optionView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"nopic"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
            [weakSelf.optionView setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
            
        }];
}


- (UIColor *)getColor:(NSString *)hexColor
{
    NSString *string = [hexColor substringFromIndex:1];//去掉#号
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    red = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 2;
    green = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 4;
    blue = [self stringToInt:[string substringWithRange:range]];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

- (int)stringToInt:(NSString *)string{
    
    unichar hex_char1 = [string characterAtIndex:0]; ////两位16进制数中的第一位(高位*16)
    int int_ch1;
    if(hex_char1 >= '0' && hex_char1 <='9')
        int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
    else
        int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
    
    unichar hex_char2 = [string characterAtIndex:1]; ///两位16进制数中的第二位(低位)
    int int_ch2;
    if(hex_char2 >= '0' && hex_char2 <='9')
        int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch2 = hex_char2-55; //// A 的Ascll - 65
    else
        int_ch2 = hex_char2-87; //// a 的Ascll - 97
    return int_ch1+int_ch2;
}



@end
